/*
 * Copyright (C) 2008 Rob Cash
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.robcash.commons.task;

import java.util.concurrent.Callable;
import javax.swing.event.EventListenerList;
import org.robcash.commons.event.TaskEvent;
import org.robcash.commons.event.TaskListener;

/**
 * Base task is a callable that also sends out task lifecycle events.
 * At the start of the {@link #call()} method, a task started notification
 * is sent to listeners. Correspondingly, just before {@link #call()} returns,
 * whether successful or not, a task completed notification is sent out to
 * listeners.
 * @author Rob
 */
public abstract class Task<V> implements Callable<V> {

    /**
     * Event listeners
     */
    protected EventListenerList listeners;

    /**
     * Create a new task
     */
    public Task() {
        listeners = new EventListenerList();
    }

    /**
     * Perform task.
     * @return Implementation specific value
     * @throws java.lang.Exception Thrown if an error occurs during execution
     * of the task
     */
    final public V call() throws Exception {
        fireTaskListenerTaskStarted(new TaskEvent(this));
        try {
            return doCall();
        }
        finally {
            fireTaskListenerTaskCompleted(new TaskEvent(this));
        }
    }

    /**
     * Implementors must override/implement this method to perform the actual
     * task. This method is called by {@link #call()}.
     * @return Implementation specific value
     * @throws java.lang.Exception Thrown if an error occurs during execution
     * of the task
     */
    protected abstract V doCall() throws Exception;

    /**
     * Add a new listener
     * @param listener Task listener
     */
    public void addTaskListener(TaskListener listener) {
        listeners.add(TaskListener.class, listener);
    }

    /**
     * Remove a new listener
     * @param listener Task listener
     */
    public void removeTaskListener(TaskListener listener) {
        listeners.remove(TaskListener.class, listener);
    }

    /**
     * Notify listeners
     * @param evt Task event to send to listeners
     */
    protected void fireTaskListenerTaskStarted(TaskEvent evt) {
        TaskListener[] taskListeners = listeners.getListeners(TaskListener.class);
        for( int index = 0; index < taskListeners.length; index++ ) {
            taskListeners[index].taskStarted(evt);
        }
    }

    /**
     * Notify listeners
     * @param evt Task event to send to listeners
     */
    protected void fireTaskListenerTaskCompleted(TaskEvent evt) {
        TaskListener[] taskListeners = listeners.getListeners(TaskListener.class);
        for( int index = 0; index < taskListeners.length; index++ ) {
            taskListeners[index].taskCompleted(evt);
        }
    }

}
